from PyQt5.QtWidgets import ( QHBoxLayout, QLabel, QPushButton,QLineEdit, QMessageBox)
from components.actiondialog import ActionDialog

class WhileLoopDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {"type": "while", "condition": True, "exit_key": "esc", "actions": []}

        # 退出按键
        key_layout = QHBoxLayout()
        key_layout.addWidget(QLabel("退出快捷键:"))
        self.key_edit = QLineEdit(self.action.get("exit_key", "esc"))
        key_layout.addWidget(self.key_edit)

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addLayout(key_layout)
        self.layout.addWidget(button)

    def accept(self):
        exit_key = self.key_edit.text()
        if not exit_key:
            QMessageBox.warning(self, "警告", "必须输入退出快捷键")
            return

        self.action = {
            "type": "while",
            "condition": True,
            "exit_key": exit_key,
            "desc": f"循环执行 (按 {exit_key} 退出)"
        }
        super().accept()
